package com.sd.view;

import java.util.Vector;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alex.books.BaseBook;
import com.sd.activity.r.R;
import com.sd.app.AppInit;
import com.sd.util.AlexStringUtil;
import com.sd.util.BookCabin;
import com.sd.util.DownloadChannel;
import com.sd.util.DownloaderEngine;

public class BookTilingAdapter extends BaseAdapter {

	
	private Vector<BaseBook> mBooks;
	private LayoutInflater mLayoutInflater;
	private OnClickListener mListener;
	private OnLongClickListener mLongListener;
	private Context mContext;
	private float mD;
	
	public BookTilingAdapter(Activity context,OnClickListener listener,OnLongClickListener longListener){
		DisplayMetrics metric = new DisplayMetrics();
	    context.getWindowManager().getDefaultDisplay().getMetrics(metric);
	    mD = metric.density;  // 屏幕密度（0.75 / 1.0 / 1.5）
		mContext = context;
		mBooks = BookCabin.getInstance().getBooks();
		mLayoutInflater = LayoutInflater.from(context);
		mListener = listener;
		mLongListener = longListener;
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mBooks.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return mBooks.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}
	
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder holder = null;
		if(convertView==null){
			holder = new ViewHolder();
			convertView = mLayoutInflater.inflate(R.layout.bookview, null);
			holder.book1 = (ImageView)convertView.findViewById(R.id.book);
			holder.book2 = (TextView)convertView.findViewById(R.id.bookname);
			holder.book3 = (ImageView)convertView.findViewById(R.id.bookmark);
			holder.download = (RelativeLayout)convertView.findViewById(R.id.download);
			
			convertView.setTag(holder);
		}else{
			holder = (ViewHolder)convertView.getTag();
		}
		
		if(mBooks.get(position).mBookMarksNumber>0){
			holder.book3.setVisibility(View.VISIBLE);
		}else{
			holder.book3.setVisibility(View.GONE);
		}
		convertView.setPadding(0, (int)(7*mD), 0, (int)(15*mD));
		if(position<mBooks.size() && mBooks.get(position)!=null){
		
	 		ImageView mBookMark = new ImageView(mContext);
			if(AlexStringUtil.isUnknownString(mBooks.get(position).mBookCover)){
				holder.book1.setImageDrawable(null);
			}else{
				holder.book1.setImageDrawable(new BitmapDrawable(AppInit.getInstance().APP_BOOKS + mBooks.get(position).mBookCover));
			}
			if(mBooks.get(position).mAttach1 != null && mBooks.get(position).mAttach1.equals("1")){
				if(mBooks.get(position).mAttach2 != null && mBooks.get(position).mAttach2.startsWith("http")){
					holder.download.setVisibility(View.VISIBLE);
					DownloadChannel download = DownloaderEngine.getInstance().getDownloadChannel(mBooks.get(position));
					if( download== null){
						DownloaderEngine.getInstance().addDownloadChannel(mBooks.get(position), holder.download);
					}else{
						download.setRelative(holder.download);
					}
				}else{
					holder.download.setVisibility(View.GONE);
				}
				holder.book2.setText("");
			}else{
				holder.download.setVisibility(View.GONE);
				holder.book2.setText(mBooks.get(position).mBookName);
			}
		}
		
		return convertView;
	}
	
	class ViewHolder {
		/**
		 * 做法名字
		 */
		ImageView book1;
		/**
		 * 做法介绍
		 */
		TextView book2;
		/**
		 * 做法加价
		 */
		ImageView book3;
		
		RelativeLayout download;
	}
	
	
	
}
